<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="所属项目ID" prop="projectId">
        <el-input v-model="queryParams.projectId" placeholder="请输入所属项目ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="关联会议ID" prop="meetingId">
        <el-input v-model="queryParams.meetingId" placeholder="请输入关联会议ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="责任人ID" prop="responsibleUserId">
        <el-input v-model="queryParams.responsibleUserId" placeholder="请输入责任人ID" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="责任人姓名" prop="responsibleUserName">
        <el-input v-model="queryParams.responsibleUserName" placeholder="请输入责任人姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="计划完成时间" prop="dueDate">
        <el-date-picker clearable v-model="queryParams.dueDate" type="date" value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择计划完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检查人ID" prop="checkerUserId">
        <el-input v-model="queryParams.checkerUserId" placeholder="请输入检查人ID" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="检查人姓名" prop="checkerUserName">
        <el-input v-model="queryParams.checkerUserName" placeholder="请输入检查人姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否完成" prop="isCompleted">
        <el-input v-model="queryParams.isCompleted" placeholder="请输入是否完成" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="实际完成时间" prop="completionTime">
        <el-date-picker clearable v-model="queryParams.completionTime" type="date" value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择实际完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['officeMeeting:meetingTask:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['officeMeeting:meetingTask:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['officeMeeting:meetingTask:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['officeMeeting:meetingTask:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="meetingTaskList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="会议名称" align="center" prop="meetingName" />
      <el-table-column label="具体事项" align="center" prop="taskDescription" />
      <!-- <el-table-column label="责任人ID" align="center" prop="responsibleUserId" /> -->
      <el-table-column label="责任人" align="center" prop="responsibleUserName" />
      <el-table-column label="计划完成时间" align="center" prop="dueDate" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.dueDate }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="检查人ID" align="center" prop="checkerUserId" /> -->
      <el-table-column label="检查人" align="center" prop="checkerUserName" />
      <el-table-column label="是否完成" align="center" prop="isCompleted">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.meeting_matter_status" :value="scope.row.isCompleted" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="实际完成时间" align="center" prop="completionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['officeMeeting:meetingTask:edit']">修改</el-button>
          <router-link :to="'/officeMeeting/meetingRecordDetail/index/' + scope.row.meetingId" class="link-type">
            <el-button size="mini" type="text" icon="el-icon-view">查看详情</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改会议事项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="会议名称" prop="meetingName">
          <el-input v-model="form.meetingName" placeholder="请输入会议名称" disabled />
        </el-form-item>
        <el-form-item label="具体事项" prop="taskDescription">
          <el-input v-model="form.taskDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="责任人" prop="responsibleUserName">
          <el-input disabled v-model="form.responsibleUserName" placeholder="请输入责任人姓名" />
        </el-form-item>
        <el-form-item label="计划完成时间" prop="dueDate">
          <el-date-picker clearable v-model="form.dueDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择计划完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检查人" prop="checkerUserName">
          <el-input disabled v-model="form.checkerUserName" placeholder="请输入检查人姓名" />
        </el-form-item>
        <el-form-item label="是否完成" prop="isCompleted">
          <el-switch v-model="form.isCompleted" :active-value="1" :inactive-value="0"
            @change="handleFormCompletedChange" />
        </el-form-item>
        <!-- <el-form-item label="实际完成时间" prop="completionTime">
          <el-date-picker clearable v-model="form.completionTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择实际完成时间" :disabled="form.isCompleted !== 1">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMeetingUNTask, listMeetingTask, getMeetingTask, delMeetingTask, addMeetingTask, updateMeetingTask } from "@/api/officeMeeting/meetingTask";

export default {
  name: "MeetingTask",
  dicts: ['meeting_format', 'meeting_matter_status'],
  data() {
    const validateCompletionTime = (rule, value, callback) => {
      if (this.form && this.form.isCompleted === 1 && !value) {
        callback(new Error("请填写实际完成时间"));
      } else {
        callback();
      }
    };
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会议事项表格数据
      meetingTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        meetingId: null,
        taskDescription: null,
        responsibleUserId: null,
        responsibleUserName: null,
        dueDate: null,
        checkerUserId: null,
        checkerUserName: null,
        isCompleted: 0,
        completionTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskDescription: [
          { required: true, message: "具体事项/任务描述不能为空", trigger: "blur" }
        ],
        responsibleUserName: [
          { required: true, message: "责任人姓名不能为空", trigger: "blur" }
        ],
        dueDate: [
          { required: true, message: "计划完成时间不能为空", trigger: "change" }
        ],
        checkerUserName: [
          { required: true, message: "检查人姓名不能为空", trigger: "blur" }
        ],
        isCompleted: [
          { required: true, message: "请选择是否完成", trigger: "change" }
        ],
        completionTime: [
          { validator: validateCompletionTime, trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 获取当前时间字符串 yyyy-MM-dd HH:mm:ss */
    getCurrentDateTime() {
      const date = new Date();
      const pad = (num) => (num < 10 ? `0${num}` : `${num}`);
      const y = date.getFullYear();
      const m = pad(date.getMonth() + 1);
      const d = pad(date.getDate());
      const h = pad(date.getHours());
      const mi = pad(date.getMinutes());
      const s = pad(date.getSeconds());
      return `${y}-${m}-${d}`;
      // return `${y}-${m}-${d} ${h}:${mi}:${s}`;
    },
    /** 是否完成切换时同步实际完成时间 */
    handleFormCompletedChange(val) {
      if (val === 1) {
        if (!this.form.completionTime) {
          this.form.completionTime = this.getCurrentDateTime();
        }
      } else {
        this.form.completionTime = null;
      }
      this.$nextTick(() => {
        if (this.$refs["form"]) {
          this.$refs["form"].validateField("completionTime");
        }
      });
    },
    /** 查询会议事项列表 */
    getList() {
      this.loading = true;
      listMeetingUNTask().then(response => {
        this.meetingTaskList = response.data;
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        meetingId: null,
        meetingName: null,
        taskDescription: null,
        responsibleUserId: null,
        responsibleUserName: null,
        dueDate: null,
        checkerUserId: null,
        checkerUserName: null,
        isCompleted: 0,
        completionTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会议事项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMeetingTask(id).then(response => {
        this.form = Object.assign({}, response.data);
        this.form.meetingName = this.form.meetingName || (row && row.meetingName) || "";
        if (this.form.dueDate) {
          this.form.dueDate = this.form.dueDate.split(' ')[0];
        }
        this.form.isCompleted = this.form.isCompleted != null ? Number(this.form.isCompleted) : 0;
        if (this.form.isCompleted !== 1) {
          this.form.completionTime = null;
        }
        this.open = true;
        this.title = "修改会议事项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMeetingTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMeetingTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会议事项编号为"' + ids + '"的数据项？').then(function () {
        return delMeetingTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('officeMeeting/meetingTask/export', {
        ...this.queryParams
      }, `meetingTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.link-type {
  text-decoration: none;
  margin-left: 16px;
}
</style>
